package cz.czu.pef.DataModelling;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.persistence.*;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cz.czu.pef.DataModelling.utils.Lazy;

@Entity
@Table(name="MACHINERIES", uniqueConstraints=@UniqueConstraint(columnNames="SERIAL_NUMBER", name="UQ_MACHINERY_SERIAL_NUMBER"))
@Inheritance(strategy=InheritanceType.JOINED)
@NamedQueries({
	@NamedQuery(name="findMachineryBySerialNo", query="SELECT m FROM Machinery m WHERE machinerySerialNo = :serialNo")
})
@ManagedBean
@ViewScoped
@Component
public class Machinery extends IdPersistenceObject
{
	private static final long serialVersionUID = 6497514274059955169L;

	@Column(name="SERIAL_NUMBER", length=32, nullable=false)
	private String machinerySerialNo;

	@ManyToOne(fetch=FetchType.EAGER, optional=true, targetEntity=Company.class, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
	@JoinTable(name="MAP_MACHINERIES_COMPANY")
	private Company companyOwner;

	@ManyToOne(fetch=FetchType.EAGER, optional=true, targetEntity=Person.class, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
	@JoinTable(name="MAP_MACHINERIES_PERSON")
	private Person personOwner;

	@ManyToOne(fetch=FetchType.EAGER, optional=true, targetEntity=MachineType.class, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
	@JoinTable(name="MAP_MACHINERIES_MACHINE_TYPE")
	private MachineType machineType;
	
	// TODO: Connect with TrainingType/RoleType???

	public Machinery(String machineSerialNo, MachineType machineType, Person personOwner, Company companyOwner)
	{
		super();
		this.setMachineType(machineType);
		this.setPersonOwner(personOwner);
		this.setCompanyOwner(companyOwner);
		this.machinerySerialNo = machineSerialNo;
	}
	public Machinery(String machineSerialNo)
	{
		super();
		this.machinerySerialNo = machineSerialNo;
	}
	public Machinery()
	{
	}

	public void setMachineSerialNo(String machineSerialNo)
	{
		this.machinerySerialNo = machineSerialNo;
	}

	public String getMachineSerialNo()
	{
		return machinerySerialNo;
	}
	
	public MachineType getMachineType()
	{
		return machineType;
	}
	
	public void setMachineType(MachineType machineType)
	{
		this.machineType = machineType;
	}
	
	public Person getPersonOwner()
	{
		return personOwner;
	}
	
	public void setPersonOwner(Person personOwner)
	{
		this.personOwner = personOwner;
	}
	
	public Company getCompanyOwner()
	{
		return companyOwner;
	}
	
	public void setCompanyOwner(Company companyOwner)
	{
		this.companyOwner = companyOwner;
	}

	@Lazy
	@Transactional(readOnly=true, propagation=Propagation.REQUIRED)
	@Override
	public String toString()
	{
		StringBuilder builder = new StringBuilder();
		builder.append("Machinery [machineSerialNo=");
		builder.append(machinerySerialNo);
		builder.append(", machineType=");
		builder.append(machineType);
		builder.append(", toString()=");
		builder.append(super.toString());
		builder.append("]");
		return builder.toString();
	}

	/**
	 * Machinery can be deleted at once (no relations).
	 */
	@Override
	public boolean canBeRemoved()
	{
		return true;
	}
}
